package com.jtjrenren.android.taxi.driver.core.setting;

import android.app.TimePickerDialog;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;


import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.jtjrenren.android.taxi.driver.bean.api.ApiDutyInfo;
import com.jtjrenren.android.taxi.driver.bean.api.CommonResult;
import com.jtjrenren.android.taxi.driver.manger.AppManger;
import com.jtjrenren.android.taxi.driver.R;
import com.jtjrenren.android.taxi.driver.bean.DriverInfo;

import com.jtjrenren.android.taxi.driver.base.BaseActivity;
import com.jtjrenren.android.taxi.driver.manger.api.ApiWorks;
import com.library.weiget.ClearEditText;
import com.library.utils.LogUtil;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/5/5.
 */
public class SettingHbActivity extends BaseActivity {
    @BindView(R.id.hb_hbdd)
    ClearEditText et_addr;
    @BindView(R.id.hb_tqsj)
    ClearEditText et_sj;
    @BindView(R.id.hb_hbsj)
    TextView tv_time;

    private String time;
    private GeoCoder mSearch;
    private LatLng mLatLng;
    private String addr, city;


    public ApiDutyInfo info;
    private DriverInfo driver;

    @Override
    public int getLayoutID() {
        return R.layout.aty_setting_hb;
    }

    @Override
    public void init() {
        if (getIntent().hasExtra("info")) {
            info = (ApiDutyInfo) getIntent().getExtras().get("info");
        }

        driver = AppManger.getInstance().driver;

        ((TextView) findViewById(R.id.title_title)).setText("换班");
        findViewById(R.id.title_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
            @Override
            public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
                if (geoCodeResult == null || geoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
                    // 没有检索到结果
                    displayShort("无效地址，请尝试输入具体一点！");
                    disProDialog();
                    return;
                }

                mLatLng = geoCodeResult.getLocation();

                setDuty();
            }

            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {

            }
        });
        city = AppManger.getInstance().currentLocation.getCity();

        refreshView(info);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mSearch != null)
            mSearch.destroy();
    }

    @OnClick(R.id.hb_save)
    public void save() {
        addr = et_addr.getText().toString().trim();

        mSearch.geocode(new GeoCodeOption().city(city).address(addr));
    }

    @OnClick(R.id.hb_hbsj)
    public void selTime() {
        showTimePickDialog();
    }

    private void refreshView(ApiDutyInfo info) {
        if (info == null)
            return;

        time=info.getChangeWorkTime();

        tv_time.setText(info.getChangeWorkTime());
        et_sj.setText(info.getForwardTime());

        String[] split = info.getDestinationAddr().split("\\^");
        if (split != null && split.length > 0)
            et_addr.setText(split[0]);

    }


    private void setDuty() {
        addr = addr + "^" + mLatLng.latitude + "^" + mLatLng.longitude;
        String dtime = et_sj.getText().toString().trim();


        if (driver == null)
            return;

        ApiWorks.setDriverRarelyDuty(driver.getWorkNo(), "2", "", addr, "", time, dtime, new ApiWorks.ResponseListener<CommonResult>() {
            @Override
            public void onResponse(CommonResult commonResult) {
                disProDialog();
                if (commonResult != null && commonResult.getCode() == 1) {
                    displayShort(getString(R.string.shezhichenggong));
                    finish();
                } else {
                    displayShort(getString(R.string.shezhishibai));
                }
            }
        });
    }

    private void showTimePickDialog() {

        LogUtil.log(SettingHbActivity.class, "show time dialog");

        Calendar calendar = Calendar.getInstance();
        int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                time = hourOfDay + ":" + (minute < 10 ? "0" : "") + minute + ":00";
                tv_time.setText(time);
            }
        }, hourOfDay, minute, true);

        timePickerDialog.show();
    }
}
